﻿Imports Tohto.Base.ComUtility
Imports Tohto.Base.CommonConst

Public Class CsvDialog
    Inherits System.Web.UI.Page

    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load

        Try
            Dim svCsv As FarPoint.Web.Spread.SheetView = DirectCast(Session(ComConst.SEE_CSV_SPREADVIEW), FarPoint.Web.Spread.SheetView)
            Dim csvFileNm As String = Util.ObjToString(Session(ComConst.SEE_CSV_FILENM))

            'MemoryStreamに内容を出力
            Dim memStream As New System.IO.MemoryStream

            svCsv.SaveTextFile(memStream, True, _
                                                FarPoint.Web.Spread.Model.IncludeHeaders.ColumnHeadersCustomOnly, _
                                                vbCr & vbLf, ",", """")
            Response.Clear()
            Response.ContentType = "application/octet-stream"
            Response.AddHeader("content-disposition", "attachment;filename=" & HttpUtility.UrlEncode(csvFileNm, System.Text.Encoding.UTF8))

            Response.BinaryWrite(memStream.ToArray())
            memStream.Flush()
            memStream.Close()

            ' バッファリングされているすべての内容をクライアントへ送信します。
            Response.Flush()
            Response.End()
        Catch ex As Exception
            SetErrorArea(ex.ToString())
        End Try

    End Sub

    Private Sub SetErrorArea(ByVal strError As String)
        Dim dt As DataTable = Nothing
        If Not IsNothing(Session(ComConst.SEE_S_M_MESSAGE)) Then
            dt = DirectCast(Session(ComConst.SEE_S_M_MESSAGE), DataTable)
        End If

        Dim dr() As DataRow = dt.Select("MSG_CD = 'CME0002'")
        Dim strScript As String = String.Empty
        strScript = "$(window.parent.document).find('#logic_message').show();"
        strScript += "$(window.parent.document).find('#logic_message').html('"
        strScript += Util.ObjToString(dr(0)("MESSAGE1"))
        strScript += "</br>"
        strError = strError.Replace(Chr(13), "")
        strError = strError.Replace(Chr(10), "</br>")
        strScript += strError
        strScript += "');"

        ClientScript.RegisterStartupScript(GetType(Page), "printError", strScript, True)
    End Sub
End Class